package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.example.demo.service.Constants;
import com.example.demo.service.LoginService;
import com.example.demo.service.MsgHandler;
import com.example.demo.util.OkhttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
public class TestController {

    @Resource
    private LoginService loginService;
    @Resource
    private MsgHandler msgHandler;
    
    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot!";
    }

    @GetMapping("/setToken")
    public String setToken() {
        return loginService.setToken(false);
    }

    @GetMapping("/setTokenForce")
    public String setTokenForce() {
        return loginService.setToken(true);
    }

    @GetMapping("/setXueqiuToken")
    public String setXueqiuToken() {
        return loginService.setXueqiuToken();
    }

    @GetMapping("/setAppId")
    public String setAppId() {
        return loginService.setAppId();
    }

    @GetMapping("/getConstants")
    public Object getConstants() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("baseUrl", Constants.baseUrl);
        jsonObject.put("token", Constants.token);
        jsonObject.put("xqAToken", Constants.xqAToken);
        jsonObject.put("appId", Constants.appId);
        jsonObject.put("qrImgBase64", Constants.qrImgBase64);
        return jsonObject;
    }

    @GetMapping("/xueqiu/batch")
    public Object batch(String params) {
        if (StrUtil.isBlank(params)) {
            params = "SH600188,SH600546,SH601225,SH516970,SH516950,SH600031";
        }
        List<String> list = List.of(params);
        return OkhttpUtil.getXueqiuBatchJSON(list);
    }

    @GetMapping("/xueqiu/suggest")
    public Object suggest(String params) {
        return OkhttpUtil.getXueqiuSuggestJSON(params);
    }

    @GetMapping("/xueqiu/cmd")
    public Object cmd(String cmd) {
        return msgHandler.handler(cmd);
    }
}